<?php

namespace app\admin\model\biz;

use think\Model;


class BizInfo extends Model
{

    

    

    // 表名
    protected $table = 'biz_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $dateFormat='Y-m-d H:i:s';

    // 追加属性
    protected $append = [

    ];

    public function getList($params){
        $EARTH=6378.137;
        $PI=3.1415926535898;
        $lat=$params['lat']??0;
        $lng=$params['lng']??0;
        $query = $this ->field("id,name,address,latitude,longitude,mobile,contacts,notice,`desc`,province,city,district,images,
         ROUND(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(".$lat."-latitude)/360),2)+COS($PI*".$lat."/180)* COS(latitude * $PI/180)*POW(SIN($PI*(".$lng."-longitude)/360),2))),2) as distance");

        $query->where(['status'=>1,'is_biz'=>1]);
        $query->page($params['page']??1);
        if(!empty($params['sk'])) $query->whereLike('name',"%{$params['sk']}%");
        if(!empty($params['id'])) $query->where('id',$params['id']);
        $query->order('distance asc');
        $res = $query->select();

        foreach ( $res as $item){
            $item['img'] = explode(',',$item['images'])[0] ?? '';
        }
        return $res;
    }

    







    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'uid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
